package org.zdd.bookstore.model.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zdd.bookstore.common.pojo.BSResult;
import org.zdd.bookstore.common.utils.BSResultUtil;
import org.zdd.bookstore.model.dao.BookInfoMapper;
import org.zdd.bookstore.model.dao.CartMapper;
import org.zdd.bookstore.model.entity.BookInfo;
import org.zdd.bookstore.model.entity.CartBook;
import org.zdd.bookstore.model.entity.Carts;
import org.zdd.bookstore.model.entity.custom.Cart;
import org.zdd.bookstore.model.entity.custom.CartItem;
import org.zdd.bookstore.model.service.ICCartService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户服务
 */
@Service
public class CCartServiceImpl implements ICCartService {

    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private BookInfoMapper bookInfoMapper;

    @Override
    public BSResult addToCart(int userId, int bookId, int buyNum) {
        Carts cart = new Carts();
        cart.setCartId(null);
        cart.setUserId(userId);
        cart.setBookId(bookId);
        cart.setBookNum(buyNum);

        int i = cartMapper.insert(cart);

        return BSResultUtil.success(cart);
    }

    @Override
    public BSResult clearCart(HttpServletRequest request, String sessionName) {
        return null;
    }

    @Override
    public BSResult deleteCartItem(int bookId, int userId) {

        cartMapper.deleteById(bookId,userId);
        return null;

    }

    @Override
    public BSResult updateBuyNum(int userId,int bookId, int newNum) {
        cartMapper.updateById(userId,bookId,newNum);
        return null;
    }

    @Override
    public BSResult checkedOrNot(Cart cart,int userId, int bookId) {

        Map<Integer, CartItem> cartItems = cart.getCartItems();
        if (cartItems.containsKey(bookId)) {
            CartItem cartItem = cartItems.get(bookId);
            if (cartItem.isChecked()) {
                //如果之前是true，那就设为false
                cartItem.setChecked(false);
                cart.setTotal(cart.getTotal() - cartItem.getSubTotal());
                cartItem.setSubTotal(0.00);
                cartMapper.checkedById(userId,bookId,0);

            } else {

                //如果之前是false，那就设为true
                cartItem.setChecked(true);
                cartItem.setSubTotal(cartItem.getBuyNum() * cartItem.getBookInfo().getPrice().doubleValue());
                cartMapper.checkedById(userId,bookId,1);
                cart.setTotal(cart.getTotal() + cartItem.getSubTotal());

            }
            System.out.println(cart.getTotal());
            return BSResultUtil.success(cart);
        } else
            return BSResultUtil.build(400, "购物车没有这本书籍!");
    }

    @Override
    public BSResult queryByUserId(int userId) {
        double total = 0;
        Cart cart = new Cart();
        List<CartBook> cartBooks =  cartMapper.selectByUserId(userId);
        if (cartBooks == null){
            return  BSResultUtil.success(null);
        }
        Map<Integer, CartItem> cartItems = new HashMap<>();
        for (CartBook cartBook:cartBooks) {
            CartItem cartItem = new CartItem();
            System.out.println(cartBook.getBookId());
            BookInfo bookInfo = new BookInfo();
            bookInfo.setBookId(cartBook.getBookId());
            bookInfo.setBookCategoryId(cartBook.getBookCategoryId());
            bookInfo.setStoreId(cartBook.getStoreId());
            bookInfo.setName(cartBook.getName());
            bookInfo.setOutline(cartBook.getOutline());
            bookInfo.setDetail(cartBook.getDetail());
            bookInfo.setPress(cartBook.getPress());
            bookInfo.setPublishDate(cartBook.getPublishDate());
            bookInfo.setSize(cartBook.getSize());
            bookInfo.setVersion(cartBook.getVersion());
            bookInfo.setAuthor(cartBook.getAuthor());
            bookInfo.setTranslator(cartBook.getTranslator());
            bookInfo.setIsbn(cartBook.getIsbn());
            bookInfo.setPrice(cartBook.getPrice());
            bookInfo.setPages(cartBook.getPages());
            bookInfo.setCatalog(cartBook.getCatalog());
            bookInfo.setMarketPrice(cartBook.getMarketPrice());
            bookInfo.setMemberPrice(cartBook.getMemberPrice());
            bookInfo.setDealMount(cartBook.getDealMount());
            bookInfo.setLookMount(cartBook.getLookMount());
            bookInfo.setDiscount(cartBook.getDiscount());
            bookInfo.setImageUrl(cartBook.getImageUrl());
            bookInfo.setStoreMount(cartBook.getStoreMount());
            bookInfo.setStoreTime(cartBook.getStoreTime());
            bookInfo.setPackStyle(cartBook.getPackStyle());
            bookInfo.setIsShelf(cartBook.getIsShelf());
            cartItem.setBuyNum(cartBook.getBookNum());
            cartItem.setBookInfo(bookInfo);
            cartItem.setSubTotal(bookInfo.getPrice().doubleValue() * cartBook.getBookNum());
            cartItems.put(cartBook.getBookId(), cartItem);
        }
        for (CartItem cartItem : cartItems.values()) {
            total += cartItem.getSubTotal();
        }
        cart.setCartItems(cartItems);
        cart.setTotal(total);
        System.out.println(cart);
        return BSResultUtil.success(cart);
    }

    @Override
    public BSResult selectById(int userId, int bookId) {
        double total = 0;
        Cart cart = new Cart();
        List<CartBook> cartBooks = cartMapper.selectById(userId, bookId);
        if (cartBooks == null){
            return  BSResultUtil.success(null);
        }
        Map<Integer, CartItem> cartItems = new HashMap<>();
        for (CartBook cartBook:cartBooks) {
            CartItem cartItem = new CartItem();
            System.out.println(cartBook.getBookId());
            BookInfo bookInfo = new BookInfo();
            bookInfo.setBookId(cartBook.getBookId());
            bookInfo.setBookCategoryId(cartBook.getBookCategoryId());
            bookInfo.setStoreId(cartBook.getStoreId());
            bookInfo.setName(cartBook.getName());
            bookInfo.setOutline(cartBook.getOutline());
            bookInfo.setDetail(cartBook.getDetail());
            bookInfo.setPress(cartBook.getPress());
            bookInfo.setPublishDate(cartBook.getPublishDate());
            bookInfo.setSize(cartBook.getSize());
            bookInfo.setVersion(cartBook.getVersion());
            bookInfo.setAuthor(cartBook.getAuthor());
            bookInfo.setTranslator(cartBook.getTranslator());
            bookInfo.setIsbn(cartBook.getIsbn());
            bookInfo.setPrice(cartBook.getPrice());
            bookInfo.setPages(cartBook.getPages());
            bookInfo.setCatalog(cartBook.getCatalog());
            bookInfo.setMarketPrice(cartBook.getMarketPrice());
            bookInfo.setMemberPrice(cartBook.getMemberPrice());
            bookInfo.setDealMount(cartBook.getDealMount());
            bookInfo.setLookMount(cartBook.getLookMount());
            bookInfo.setDiscount(cartBook.getDiscount());
            bookInfo.setImageUrl(cartBook.getImageUrl());
            bookInfo.setStoreMount(cartBook.getStoreMount());
            bookInfo.setStoreTime(cartBook.getStoreTime());
            bookInfo.setPackStyle(cartBook.getPackStyle());
            bookInfo.setIsShelf(cartBook.getIsShelf());
            cartItem.setBuyNum(cartBook.getBookNum());
            cartItem.setBookInfo(bookInfo);
            cartItem.setSubTotal(bookInfo.getPrice().doubleValue() * cartBook.getBookNum());
            cartItems.put(cartBook.getBookId(), cartItem);
        }
        for (CartItem cartItem : cartItems.values()) {
            total += cartItem.getSubTotal();
        }
        cart.setCartItems(cartItems);
        cart.setTotal(total);

        return BSResultUtil.success(cart);
    }

    @Override
    public BSResult deleteByOrder(int userId) {
        cartMapper.deleteByOrder(userId);
        return null;
    }

    @Override
    public BSResult initCart(int userId) {
        cartMapper.initCart(userId);
        return null;
    }

}
